/*
 ============================================================================
 Name		: TestDBAppUi.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : CTestDBAppUi implementation
 ============================================================================
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>
#include <hlplch.h>

#include <TestDB_0xE8820F2E.rsg>

#include "TestDB_0xE8820F2E.hlp.hrh"
#include "TestDB.hrh"
#include "TestDB.pan"
#include "TestDBApplication.h"
#include "TestDBAppUi.h"
#include "TestDBAppView.h"

#include "mk4.h"
#include "mk4str.h"

_LIT( KFileName, "C:\\private\\E8820F2E\\TestDB.txt" );
_LIT( KText, "Hello World!");

// ============================ MEMBER FUNCTIONS ===============================

int FieldIntValue(c4_View* view, char* fieldname, int pos)
{
	c4_IntProp intfield(fieldname);
	int value = intfield((*view)[pos]);
	return value;
}

void SetRowRefIntValue(c4_RowRef rowref, char* fieldname, int value)
{
	c4_IntProp intfield(fieldname);
	intfield(rowref) = value;
}

void SetRowRefStrValue(c4_RowRef rowref, char* fieldname, const TDesC& value)
{
	//以后要用hbuf8来处理
	TBuf8<256> str8;
	str8.Copy(value.Left(256));
	
	char* p = (char *)str8.Ptr();
	p[value.Length()] = 0;	
	
	c4_String str(p);
	c4_StringProp strfield(fieldname);
	strfield(rowref) = str;
}

const TPtrC8 FieldUtf8Value(c4_View* view, char* fieldname, int pos)
{
	c4_BytesProp binfield(fieldname);
	c4_Bytes bin;
	bin = binfield(((*view)[pos]));

	const int len = bin.Size();
	if(len == 0) return _L8("");
	
	const TPtrC8 ptr8((TUint8*)bin.Contents(), len);
	return ptr8;
}

int FindBy2Int(c4_View* view, char* field1, int keyvalue1, char* field2, int keyvalue2)
{
	int pos = -1;
	c4_IntProp intfield1(field1);
	c4_IntProp intfield2(field2);	
	pos = view->Find(intfield1[keyvalue1]+intfield2[keyvalue2]);

	return pos;
}


// -----------------------------------------------------------------------------
// CTestDBAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
bool ggg = false;
extern int gBinfilepos;
#include "univ.h"
#include "persist.h"
void CTestDBAppUi::ConstructL()
	{
	// Initialise app UI with standard value.
	BaseConstructL(CAknAppUi::EAknEnableSkin);

	// Create view object
	iAppView = CTestDBAppView::NewL(ClientRect());


//	{
//	RFile file;
//	int err = file.Open(CCoeEnv::Static()->FsSession(), _L("c:\\data\\test\\info.bdb"), EFileRead);
//	CleanupClosePushL(file);
//	if (KErrNone != err)
//		{
//		CleanupStack::PopAndDestroy(1); // file
//		return;
//		}
//
//	
//		int pos = 250725;
//		for(int i = 0; i < 250; i++)
//		{
//		file.Seek(ESeekStart, pos);
//		char* data = new char[4096];
//		TPtr8 buf((TUint8*)data, 4096, 4096);
//		TInt res = file.Read(buf, 4096);
////		TInt res = _rfile.Read(pos, buf, len_);
//		TBuf<100> tt;
//		int fsize;
//		file.Size(fsize);
//		tt.Format(_L("pos:%d,i:%d,res:%d"), pos, i,res);
//		CEikonEnv::Static()->InfoWinL(_L("debug"),tt);
//		pos += 4096;
//		}
//		CleanupStack::PopAndDestroy(1); // outputFileStream, file
//	}	
//return;
	char infopath[] = "c:\\data\\test\\test.bdb";
	c4_Storage* iDBInfo = new c4_Storage(infopath, true);//?????	
	
	c4_View view = iDBInfo->View("PageImages");
	
	c4_Row row;

	int size = view.GetSize();	
	TBuf<10> tt;
	tt.Num(size);	
	CEikonEnv::Static()->InfoWinL(_L("size"), tt);
	
//	//save
	for(int i = 0 ; i < 1; i ++)
	{
		c4_Bytes data("abc",50000);
		
		c4_BytesProp bin("data");
		c4_Row row;
		
	    SetRowRefIntValue(row, "id", 100);
	    SetRowRefIntValue(row, "pageid", 999);
	    SetRowRefIntValue(row, "orderid", 88);
	    SetRowRefStrValue(row, "imagefile", _L("filename"));
		
		
		bin(row) = data;
		//c4_String str("ddd");
		//c4_StringProp strfield("imagefile");
		//strfield(row) = str;		
		view.Add(row);		
		//iDBInfo->Commit();
//		view.Persist()->TestMe(false);
	}
	iDBInfo->Commit();	
	return;
	
	size = view.GetSize();	



	for(int i = 0; i < size; i++)
	{
		//TPtrC8 ptr3 = FieldUtf8Value(&view, "data", i);
		//view.Persist()->TestMe(false);
	}

//	TPtrC8 ptr = FieldUtf8Value(&view, "data", size-1);
	c4_BytesProp binfield("data");
	c4_BytesRef bin = binfield(view[size-1]);

	
	int sss = gBinfilepos;//ptr.Length();
	//iDBInfo->Commit();	  
	tt.Num(sss);		
	CEikonEnv::Static()->InfoWinL(_L("pageid"), tt);			

//ggg = true;	
//	c4_Bytes bin = binfield(view[0]);

//	
//	int lastid = (size == 0)?0:FieldIntValue(&view, "id", size-1);
//
//	SetRowRefIntValue(row, "id", 100);
//	view.Add(row);
//	
//	iDBInfo->Commit();
//	tt.Num(view.GetSize());	
//	CEikonEnv::Static()->InfoWinL(_L("newsize"), tt);
	
	delete iDBInfo;	

	}
// -----------------------------------------------------------------------------
// CTestDBAppUi::CTestDBAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CTestDBAppUi::CTestDBAppUi()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CTestDBAppUi::~CTestDBAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CTestDBAppUi::~CTestDBAppUi()
	{
	if (iAppView)
		{
		delete iAppView;
		iAppView = NULL;
		}

	}

// -----------------------------------------------------------------------------
// CTestDBAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CTestDBAppUi::HandleCommandL(TInt aCommand)
	{
	switch (aCommand)
		{
		case EEikCmdExit:
		case EAknSoftkeyExit:
			Exit();
			break;

		case ECommand1:
			{

			// Load a string from the resource file and display it
			HBufC* textResource = StringLoader::LoadLC(R_COMMAND1_TEXT);
			CAknInformationNote* informationNote;

			informationNote = new (ELeave) CAknInformationNote;

			// Show the information Note with
			// textResource loaded with StringLoader.
			informationNote->ExecuteLD(*textResource);

			// Pop HBuf from CleanUpStack and Destroy it.
			CleanupStack::PopAndDestroy(textResource);
			}
			break;
		case ECommand2:
			{
			RFile rFile;

			//Open file where the stream text is
			User::LeaveIfError(rFile.Open(CCoeEnv::Static()->FsSession(),
					KFileName, EFileStreamText));//EFileShareReadersOnly));// EFileStreamText));
			CleanupClosePushL(rFile);

			// copy stream from file to RFileStream object
			RFileReadStream inputFileStream(rFile);
			CleanupClosePushL(inputFileStream);

			// HBufC descriptor is created from the RFileStream object.
			HBufC* fileData = HBufC::NewLC(inputFileStream, 32);

			CAknInformationNote* informationNote;

			informationNote = new (ELeave) CAknInformationNote;
			// Show the information Note
			informationNote->ExecuteLD(*fileData);

			// Pop loaded resources from the cleanup stack
			CleanupStack::PopAndDestroy(3); // filedata, inputFileStream, rFile
			}
			break;
		case EHelp:
			{

			CArrayFix<TCoeHelpContext>* buf = CCoeAppUi::AppHelpContextL();
			HlpLauncher::LaunchHelpApplicationL(iEikonEnv->WsSession(), buf);
			}
			break;
		case EAbout:
			{

			CAknMessageQueryDialog* dlg = new (ELeave) CAknMessageQueryDialog();
			dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
			HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
			dlg->QueryHeading()->SetTextL(*title);
			CleanupStack::PopAndDestroy(); //title
			HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
			dlg->SetMessageTextL(*msg);
			CleanupStack::PopAndDestroy(); //msg
			dlg->RunLD();
			}
			break;
		default:
			Panic(ETestDBUi);
			break;
		}
	}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CTestDBAppUi::HandleStatusPaneSizeChange()
	{
	iAppView->SetRect(ClientRect());
	}

CArrayFix<TCoeHelpContext>* CTestDBAppUi::HelpContextL() const
	{
#warning "Please see comment about help and UID3..."
	// Note: Help will not work if the application uid3 is not in the
	// protected range.  The default uid3 range for projects created
	// from this template (0xE0000000 - 0xEFFFFFFF) are not in the protected range so that they
	// can be self signed and installed on the device during testing.
	// Once you get your official uid3 from Symbian Ltd. and find/replace
	// all occurrences of uid3 in your project, the context help will
	// work. Alternatively, a patch now exists for the versions of 
	// HTML help compiler in SDKs and can be found here along with an FAQ:
	// http://www3.symbian.com/faq.nsf/AllByDate/E9DF3257FD565A658025733900805EA2?OpenDocument
	CArrayFixFlat<TCoeHelpContext>* array = new (ELeave) CArrayFixFlat<
			TCoeHelpContext> (1);
	CleanupStack::PushL(array);
	array->AppendL(TCoeHelpContext(KUidTestDBApp, KGeneral_Information));
	CleanupStack::Pop(array);
	return array;
	}

// End of File
